import Vue from 'vue'
import Router from 'vue-router'
import Entry from './entry'

Vue.use(Router)

const { routeBaseUrl } = window.projectConf

const router = new Router({
  mode: 'history',
  base: routeBaseUrl,
  routes: [
    ...Object.values(Entry),
  ]
})

router.beforeEach((to, from, next) => {
  // chrome
  document.body.scrollTop = 0
  // firefox
  document.documentElement.scrollTop = 0
  // safari
  window.pageYOffset = 0

  // 设置网页title值
  window.document.title = (to.meta.title).toString()
  // 如果path没有变化，只是hash改变，则不进行导航
  if (to.path === from.path) {
    next(false)
  } else {
    next()
  }
})

export default router
